package com.caine.proxy.jdk.demo01;

import java.lang.reflect.Proxy;

/**
 * @Author: CaineZhu
 * @Description:
 * @Date: Created in 16:13 2020/11/4
 * @Modified By: 基于接口的代理
 */
public class JdkproxyTest {
    public static void main(String[] args) {
        TestService test = createProxy(TestService.class);
        test.say();
    }

    public static <T> T createProxy(Class<T> targetInterface) {
        if (!targetInterface.isInterface()) {
            throw new IllegalStateException("targetInterface必须是接口类型!");
        }
        return (T) Proxy.newProxyInstance(JdkproxyTest.class.getClassLoader(), new Class[]{targetInterface}, new TestInvocationHandler());
    }
}
